package inf3791.calendarsystem.others.panels.calendar;

import inf3791.calendarsystem.shared.Appointment;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JTable;

/**
 *
 * @author Jonas Weber
 */
public class AppointmentWidget extends javax.swing.JPanel implements javax.swing.table.TableCellRenderer {

    private Appointment appointment;

    public AppointmentWidget() {
        initComponents();
        setAppointment(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        timeLabel = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();
        locLabel = new javax.swing.JLabel();
        socLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(381, 50));

        timeLabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        timeLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        timeLabel.setText("00:00");

        titleLabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        titleLabel.setText("Title");

        locLabel.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        locLabel.setText("Location");
        locLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        socLabel.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        socLabel.setText("SoC-Name");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(socLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 210, Short.MAX_VALUE))
                    .addComponent(locLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timeLabel)
                    .addComponent(titleLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(socLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(locLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel locLabel;
    private javax.swing.JLabel socLabel;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the appointment
     */
    public final Appointment getAppointment() {
        return appointment;
    }

    /**
     * @param appointment the appointment to set
     */
    public final void setAppointment(Appointment appointment) {
        this.appointment = appointment;

        DateFormat df = new SimpleDateFormat("HH:mm");

        if (appointment != null) {
            timeLabel.setText(df.format(appointment.getTime()));
            titleLabel.setText(appointment.getTitle());
            socLabel.setText("undefined");
            locLabel.setText(appointment.getDescription());
        } else {
            timeLabel.setText("00:00");
            titleLabel.setText("---");
            socLabel.setText("");
            locLabel.setText("");
        }
    }

    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object o, boolean isSel, boolean hasFoc, int i, int i1) {
        setAppointment((Appointment) o);

        return this;
    }
}
